# constructProj/jsonConfig.py
# -*-coding:utf-8 -*-
# @file       jsonConfig.py
# @author     yearnext
# @version    1.0.0
# @date       2021-11-19
# @brief      constructProj jsonConfig.py component
import os
import sys
import traceback
from PyQt5 import QtCore, QtGui, QtWidgets
from moudles.ui.AlphaTool import uiAlphaTool
from moudles.tool import tTime, tLog
from moudles.application import AplhaToolsManagement
from moudles.peripherals import tSerialThread
from moudles.widget import *

class AlphaToolWidget(QtWidgets.QWidget, uiAlphaTool):
    def __init__(self, argv, parent=None):
        super().__init__(parent)

        # UI初始化
        self.uiInit()

        # 输入参数初始化
        self.inputParamInit(argv)

        # 串口初始化
        self.serialInit()

        # 信号槽初始化
        self.sigSlotInit()

        tLog.putThreadId(self.__class__.__name__)

    # UI初始化
    def uiInit(self):
        # UI 初始化
        self.setupUi(self)

        # 添加Serial配置Widget
        self.serialConfigWidget = SerialConfigWidget(self)
        self.serialConfigLayout.addWidget(self.serialConfigWidget)

        # 添加Serial发送Widget
        self.serialSendWidget = SerialSendWidget(self)
        self.serialSendLayout.addWidget(self.serialSendWidget)

        # 设置当前设备信息
        self.serialInfoWidget = SerialInfoWidget(self)
        self.curInfoLayout.addWidget(self.serialInfoWidget)

        # 设置tool widget
        self.tManagement = AplhaToolsManagement()
        self.setToolsWidget(self.tManagement.getWidgets())

    def setToolsWidget(self, widgetList):
        # 移除全部tab
        self.tabWidget.clear()

        print(widgetList)

        # 设置tab
        for temp in widgetList:
            toolWidget = temp(self.tabWidget)
            self.tabWidget.addTab(toolWidget, toolWidget.getToolName())

        self.tabWidget.setCurrentIndex(0)

    # 输入参数初始化
    def inputParamInit(self, argv):
        tLog.putFuncArg(argv)

    def serialInit(self):
        # 设置串口实例
        self.serial = tSerialThread()

    def sigSlotInit(self):
        # print('signal solt init')
        self.serialConfigWidget.sigOpen.connect(self.serial.portOpen)
        self.serialConfigWidget.sigClose.connect(self.serial.portClose)
        self.serial.sigPortChanged.connect(self.portChangedProcess)
        self.serial.sigRecvDone.connect(self.recvProcess)

        self.clearDataButton.clicked.connect(self.clearTranfserData)

        self.tabWidget.currentChanged.connect(self.tManagement.setCurIndex)

    def portChangedProcess(self, val:int):
        if val == -1:
            # print('port open failed')
            # tLog.putInfo('port open failed')
            self.serialConfigWidget.setPortStatus(val)
        elif val == 1:
            # tLog.putInfo('port open success')
            self.serialConfigWidget.setPortStatus(val)
        elif val == 0:
            # tLog.putInfo('port closed')
            self.serialConfigWidget.setPortStatus(val)

    def recvProcess(self, recvData, packetLen, recvCnt):
        # print(recvCnt, packetLen, recvData)
        self.curInfoWidget.setRxCnt(recvCnt)
        self.curInfoWidget.setRecvTips()
        self.putRecvInfoToLog(recvData)

    def clearTranfserData(self):
        self.serial.clearTransferCount()
        self.curInfoWidget.clearTransferCount()
        self.logPut.clear()

    def putRecvInfoToLog(self, recvData):
        # 输出日志信息
        dataStr = ''
        for i in recvData:
            if self.recvHexMode.isChecked() == True:
                dataStr += str('%02x ' % (i)).upper()
            else:
                dataStr += str('%c' % (i))

        dataStr = dataStr[:-1]

        if self.tickDispCheckBox.isChecked() == True:
            info = '[' + tTime.getCurTime(1) + ']' + '收<-◆' + dataStr
        else:
            info = dataStr

        self.logPut.append(info)
